package com.nathan.crm.service;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.nathan.base.BaseService;
import com.nathan.crm.dao.CustomerReprieveMapper;
import com.nathan.crm.utils.AssertUtil;
import com.nathan.crm.vo.CustomerReprieve;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class CustomerReprieveService extends BaseService<CustomerReprieve,Integer> {
    @Resource
    private CustomerReprieveMapper customerReprieveMapper;

    /**
     * 添加暂缓措施
     * @param customerReprieve
     */
    @Transactional
    public void addCustomerRep(CustomerReprieve customerReprieve) {
        /* 参数校验 */
        AssertUtil.isTrue(StringUtils.isBlank(customerReprieve.getMeasure()),"请输入暂缓措施！");

        /* 设置默认值 */
        customerReprieve.setCreateDate(new Date());
        customerReprieve.setUpdateDate(new Date());

        /* 执行添加 */
        AssertUtil.isTrue(customerReprieveMapper.insertSelective(customerReprieve) != 1,"添加暂缓数据失败！");
    }

    /**
     * 修改暂缓措施
     * @param customerReprieve
     */
    @Transactional
    public void updateCustomerRep(CustomerReprieve customerReprieve) {
        /* 参数校验 */
        AssertUtil.isTrue(StringUtils.isBlank(customerReprieve.getMeasure()),"请输入暂缓措施！");
        AssertUtil.isTrue(customerReprieve.getId() == null,"需要修改的数据不存在！");

        /* 执行修改 */
        AssertUtil.isTrue(customerReprieveMapper.updateByPrimaryKeySelective(customerReprieve) != 1,"修改暂缓措施失败！");
    }

    /**
     * 根据id删除暂缓数据
     * @param id
     */
    @Transactional
    public void deleteCustomerRepById(Integer id) {
        /* 参数校验 */
        AssertUtil.isTrue(id == null,"要删除的数据不存在！");
        CustomerReprieve customerReprieve = customerReprieveMapper.selectByPrimaryKey(id);
        AssertUtil.isTrue(customerReprieve == null,"要删除的数据不存在！");

        /* 执行删除 */
        customerReprieve.setIsValid(0);
        AssertUtil.isTrue(customerReprieveMapper.updateByPrimaryKeySelective(customerReprieve) != 1,"删除失败！");
    }
}
